package com.learn.springcloud.hystrixexplainindetailconsumer.exception.handle.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.client.RestTemplate;

/**
 * @Time : 2018/8/3 16:13
 * @Version : 1.0
 * @Author : hongyan.li@hand-china.com
 * Copyright @ 2000 Shanghai Hand Co. Ltd.
 * All right reserved.
 */
public class UserServiceException {

    private RestTemplate restTemplate;

    public UserServiceException(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @HystrixCommand(fallbackMethod = "sayHelloFallback")
    public String sayHelloNoIgnoreException(String name) {
        System.out.println("UserServiceException Running : " + name);
        String url = "http://hystrix-explain-in-detail-provider/request/command/say/hello?name={1}";
        boolean flag = true;
        if(flag){
            throw new RuntimeException();
        }
        return restTemplate.getForObject(url, String.class, name);
    }

    @HystrixCommand(fallbackMethod = "sayHelloFallback", ignoreExceptions = {RuntimeException.class})
    public String sayHelloWithIgnoreException(String name) {
        System.out.println("UserServiceException Running : " + name);
        String url = "http://hystrix-explain-in-detail-provider/request/command/say/hello?name={1}";
        boolean flag = true;
        if(flag){
            throw new RuntimeException();
        }
        return restTemplate.getForObject(url, String.class, name);
    }

    private String sayHelloFallback(String name){
        return "error, " + name;
    }

}
